package com.example.petmall.controller;

import com.example.petmall.entity.CartItem;
import com.example.petmall.repository.CartItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@CrossOrigin
public class CartController {
    @Autowired
    private CartItemRepository cartItemRepository;

    @GetMapping("/cart")
    public List<CartItem> getCart(@RequestParam Integer userId) {
        return cartItemRepository.findByUserId(userId);
    }

    @PostMapping("/cart")
    public CartItem addToCart(@RequestBody CartItem cartItem) {
        return cartItemRepository.save(cartItem);
    }

    @DeleteMapping("/cart/{id}")
    public void removeFromCart(@PathVariable Integer id) {
        cartItemRepository.deleteById(id);
    }
}    